﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using WeifenLuo.WinFormsUI.Docking;
using com.dz.ct.components;
using com.dz.ct.common;
using com.dz.ct.business;

namespace com.dz.ct.ui
{
    public partial class FrmSiteTree : DockContent
    {
        /// <summary>
        /// 触发发送消息的事件
        /// </summary>
        public event SendMsgEventHandler SendMsg;

        /// <summary>
        /// 数据交互对象
        /// </summary>
        DBManager m_db_manager = new DBManager();

        /// <summary>
        /// 盗版小说站点列表
        /// </summary>
        DataSet m_ds_small_site = null;

        /// <summary>
        /// 线程管理
        /// </summary>
        Dictionary<int, Thread> m_dic = new Dictionary<int, Thread>();

        /// <summary>
        /// 显示处理回送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DisplayMsg(object sender, SendMsgEventArgs e)
        {
            if (SendMsg != null)
            {
                SendMsg(this, e);
            }

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmSiteTree()
        {
            InitializeComponent();

        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSiteTree_Load(object sender, EventArgs e)
        {
            BindSiteList();
        }

        /// <summary>
        /// 绑定站点列表
        /// </summary>
        private void BindSiteList()
        {
            m_ds_small_site = m_db_manager.GetSmallBookSiteList();

            treeSite.Nodes.Clear();

            TreeNode t_root = new TreeNode("小说站点列表");
            t_root.Tag = "root";
            t_root.ImageIndex = 0;
            t_root.SelectedImageIndex = 1;
            treeSite.Nodes.Add(t_root);

            TreeNode t_bigsite = new TreeNode("正版站点");
            t_bigsite.Tag = "bigsite";
            t_bigsite.ImageIndex = 0;
            t_bigsite.SelectedImageIndex = 1;
            t_root.Nodes.Add(t_bigsite);

            TreeNode t_bigsiteitem = new TreeNode("起点");
            t_bigsiteitem.Tag = "bigsiteitem";
            t_bigsiteitem.ToolTipText = "www.qidian.com";
            t_bigsiteitem.ImageIndex = 2;
            t_bigsiteitem.Name = "0";
            t_bigsiteitem.SelectedImageIndex = 2;
            t_bigsite.Nodes.Add(t_bigsiteitem);

            t_bigsiteitem = new TreeNode("晋江文学城");
            t_bigsiteitem.Tag = "bigsiteitem";
            t_bigsiteitem.ToolTipText = "www.jjwxc.net";
            t_bigsiteitem.ImageIndex = 2;
            t_bigsiteitem.Name = "6";
            t_bigsiteitem.SelectedImageIndex = 2;
            t_bigsite.Nodes.Add(t_bigsiteitem);

            TreeNode t_smallsite = new TreeNode("盗版站点");
            t_smallsite.Tag = "smallsite";
            t_smallsite.ImageIndex = 0;
            t_smallsite.SelectedImageIndex = 1;
            t_root.Nodes.Add(t_smallsite);


            if (m_ds_small_site != null && m_ds_small_site.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow t_data_row in m_ds_small_site.Tables[0].Rows)
                {
                    TreeNode t_smallsiteitem = new TreeNode(t_data_row["siteName"].ToString());
                    t_smallsiteitem.Tag = "smallsiteitem";
                    t_smallsiteitem.Name = t_data_row["siteId"].ToString();
                    t_smallsiteitem.ImageIndex = 2;
                    t_smallsiteitem.SelectedImageIndex = 2;
                    t_smallsite.Nodes.Add(t_smallsiteitem);
                }
            }


            TreeNode t_baidu = new TreeNode("百度");
            t_baidu.Tag = "smallsiteitem";
            t_baidu.Name = "10";
            t_baidu.ImageIndex = 2;
            t_baidu.SelectedImageIndex = 2;
            t_smallsite.Nodes.Add(t_baidu);
        }

        /// <summary>
        /// 设置菜单是否可见
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctmsTree_Opening(object sender, CancelEventArgs e)
        {
            tlsm_add_site.Enabled = false;
            tlsm_all_book_collect.Enabled = false;
            tlsm_collect_stop.Enabled = false;
          
            tlsm_site_delete.Enabled = false;
            tlsm_site_property.Enabled = false;
            tlsm_chapter_url.Enabled = false;
       
            tlsm_review.Enabled = false;
            tlsm_book_collect.Enabled = true;
            tlsm_reflash.Enabled = true;
            if (treeSite.SelectedNode == null || treeSite.SelectedNode.Tag == null)
                return;

            //正版站点智能收录小说信息和普通章节
            if (treeSite.SelectedNode.Tag.ToString().Equals("bigsiteitem"))
            {
          
                tlsm_review.Enabled = true;
                tlsm_book_collect.Enabled = true;
                tlsm_review.Enabled = true;
                if (treeSite.SelectedNode.Name != null && m_dic.ContainsKey(Convert.ToInt32(treeSite.SelectedNode.Name.ToString())))
                {
                    tlsm_collect_stop.Enabled = true;
                }
            }
            else if (treeSite.SelectedNode.Tag.ToString().Equals("smallsiteitem"))
            {
                tlsm_chapter_url.Enabled = true;
              
                if (treeSite.SelectedNode.Name != null && m_dic.ContainsKey(Convert.ToInt32(treeSite.SelectedNode.Name.ToString())))
                {
                    tlsm_collect_stop.Enabled = true;
                }
                
                tlsm_site_delete.Enabled = true;
                tlsm_all_book_collect.Enabled = true;
                tlsm_site_property.Enabled = true;
            
            }
            else if (treeSite.SelectedNode.Tag.ToString().Equals("smallsite"))
            {
                tlsm_add_site.Enabled = true;
            }
        }

        /// <summary>
        /// 小说站点属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_site_property_Click(object sender, EventArgs e)
        {
          
            if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                return;

            DataRow[] t_data_row = m_ds_small_site.Tables[0].Select(string.Format("siteid={0}", treeSite.SelectedNode.Name.ToString()));

            if (t_data_row != null)
            {
                FrmSite t_frm = new FrmSite();
                t_frm.m_data_row = t_data_row[0];
                t_frm.ShowDialog();
            }


        }

        /// <summary>
        /// 天几大盗版站点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_add_site_Click(object sender, EventArgs e)
        {
            FrmSite t_frm = new FrmSite();

            DialogResult dlr = t_frm.ShowDialog();
            if (dlr == DialogResult.OK)
            {
                BindSiteList();
                treeSite.ExpandAll();
            }
        }

        /// <summary>
        /// 删除站点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_site_delete_Click(object sender, EventArgs e)
        {
            if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                return;
            int t_site_id = 0;
            if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                t_site_id = 0;

            int t_ret = m_db_manager.DeleteSmallSite(t_site_id);
            if (t_ret > 0)
            {
                BindSiteList();
                treeSite.ExpandAll();

            }
            else
            {
                MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 收集所有书的所有章节
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_all_book_collect_Click(object sender, EventArgs e)
        {
            FrmConfig t_frm = new FrmConfig();
            DialogResult dlr = t_frm.ShowDialog();
            if (dlr == DialogResult.OK)
            {
                if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                    return;
                int t_site_id = 0;
                if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                    t_site_id = 0;
                Thread thd = null;
                if (t_site_id == 1)
                {
                    ZCNCollectManager t_book_mgr = new ZCNCollectManager();
                    t_book_mgr.ThreadCount = t_frm.ThreadCount;
                    t_book_mgr.BookSQL = t_frm.BookSQL;
            
                    t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                    thd = new Thread(t_book_mgr.StartCollectChapter);
                }
                else if (t_site_id == 2)
                {
                    HuaiXiuCollectManager t_book_mgr = new HuaiXiuCollectManager();
                    t_book_mgr.ThreadCount = t_frm.ThreadCount;
                    t_book_mgr.BookSQL = t_frm.BookSQL;
                    t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                     thd = new Thread(t_book_mgr.StartCollectChapter);
                }
                if (t_site_id == 10)
                {
                    BookCollectManager t_book_mgr = new BookCollectManager();
                    t_book_mgr.ThreadCount = t_frm.ThreadCount;
                    t_book_mgr.BookSQL = t_frm.BookSQL;
                    t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                 
                    thd = new Thread(t_book_mgr.StartBaiDuSearchCahapter);
                }
                if (m_dic.ContainsKey(t_site_id) == false)
                {
                    m_dic.Add(t_site_id, thd);
                }
                thd.Start();

            }
        }

        /// <summary>
        /// 停止收录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_collect_stop_Click(object sender, EventArgs e)
        {
            if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                return;
            int t_site_id = 0;
            if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                t_site_id = 0;

            if (m_dic.ContainsKey(t_site_id))
            {
                Thread thd = m_dic[t_site_id];
        
                if (thd != null)
                {
                    thd.Abort();
                }
                  
                
            }
        }

        /// <summary>
        /// 收录新书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_book_collect_Click(object sender, EventArgs e)
        {
    
          
            FrmBook t_frm = new FrmBook();

            DialogResult dlr = t_frm.ShowDialog();

            if (dlr == DialogResult.OK)
            {
                if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                    return;

                int t_site_id = 0;
                if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                    t_site_id = 0;
                BookCollectManager t_book_mgr = new BookCollectManager();
                t_book_mgr.BookUrl = t_frm.BookUrl;
                t_book_mgr.ComName = treeSite.SelectedNode.ToolTipText.ToString();
                t_book_mgr.StartPageIndex = t_frm.StartPageIndex;
                t_book_mgr.EndPageIndex = t_frm.EndPageIndex;
                t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

                Thread thd = new Thread(t_book_mgr.StartCollectBook);
                if (m_dic.ContainsKey(t_site_id) == false)
                {
                    m_dic.Add(t_site_id, thd);
                }
                thd.Start();
            }
        }


        /// <summary>
        /// 开始收录评论
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_review_Click(object sender, EventArgs e)
        {
            FrmReview t_frm = new FrmReview();
            DialogResult dlr = t_frm.ShowDialog();
            if (dlr == DialogResult.OK)
            {
                int t_site_id = 0;
                if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                    t_site_id = 0;
                if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                    return;

                BookCollectManager t_book_mgr = new BookCollectManager();
                t_book_mgr.SiteId = t_site_id;
                t_book_mgr.BookSQL = t_frm.BookSQL;
                t_book_mgr.ComName = treeSite.SelectedNode.ToolTipText.ToString();
                t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

                Thread thd = new Thread(t_book_mgr.StartCollectReview);
                if (m_dic.ContainsKey(t_site_id) == false)
                {
                    m_dic.Add(t_site_id, thd);
                }
                thd.Start();
            }
        }

        /// <summary>
        /// 寻找小说章节地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_chapter_url_Click(object sender, EventArgs e)
        {
            int t_site_id = 0;
            if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                t_site_id = 0;

            DataRow[] t_data_row = m_ds_small_site.Tables[0].Select(string.Format("siteid={0}", treeSite.SelectedNode.Name.ToString()));
            if (t_data_row == null)
                return;

            FrmBook t_frm = new FrmBook(t_data_row[0]["bookListUrl"].ToString());

            DialogResult dlr = t_frm.ShowDialog();

            if (dlr == DialogResult.OK)
            {
                if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                    return;
                Thread thd = null;
                if (t_site_id == 1)
                {
                    ZCNCollectManager t_book_mgr = new ZCNCollectManager();
                    t_book_mgr.SiteConfig = t_data_row[0];
                    t_book_mgr.BookUrl = t_frm.BookUrl;
                    t_book_mgr.StartPageIndex = t_frm.StartPageIndex;
                    t_book_mgr.EndPageIndex = t_frm.EndPageIndex;
                    t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

                    thd = new Thread(t_book_mgr.StartFindBookChapter);
                }
                else if (t_site_id == 2)
                {
                    HuaiXiuCollectManager t_book_mgr = new HuaiXiuCollectManager();
                    t_book_mgr.SiteConfig = t_data_row[0];
                    t_book_mgr.BookUrl = t_frm.BookUrl;
                    t_book_mgr.StartPageIndex = t_frm.StartPageIndex;
                    t_book_mgr.EndPageIndex = t_frm.EndPageIndex;
                    t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

                    thd = new Thread(t_book_mgr.StartFindBookChapter);
                }
                if (m_dic.ContainsKey(t_site_id) == false)
                {
                    m_dic.Add(t_site_id, thd);
                }
                thd.Start();
            }
        }

     
        private void tlsm_reflash_Click(object sender, EventArgs e)
        {
            BindSiteList();
        }

        /// <summary>
        /// 下载小说封面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_icon_down_Click(object sender, EventArgs e)
        {
            if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                return;
            int t_site_id = 0;
            if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                t_site_id = 0;

            BookCollectManager t_book_mgr = new BookCollectManager();
            t_book_mgr.ComName = treeSite.SelectedNode.ToolTipText.ToString();
            t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

            Thread thd = new Thread(t_book_mgr.StartDownLoadImage);
            if (m_dic.ContainsKey(t_site_id) == false)
            {
                m_dic.Add(t_site_id, thd);
            }
            thd.Start();
        }

        /// <summary>
        /// 引擎收录章节
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlsm_search_chapter_Click(object sender, EventArgs e)
        {
            FrmConfig t_frm = new FrmConfig();

            DialogResult dlr = t_frm.ShowDialog();

            if (dlr == DialogResult.OK)
            {
                if (treeSite.SelectedNode == null || treeSite.SelectedNode.Name == null)
                    return;
                int t_site_id = 0;
                if (!int.TryParse(treeSite.SelectedNode.Name.ToString(), out t_site_id))
                    t_site_id = 0;

                GoogleCollectManager t_book_mgr = new GoogleCollectManager();
                t_book_mgr.SiteId = t_site_id;
                t_book_mgr.BookSQL = t_frm.BookSQL;
                t_book_mgr.ThreadCount = t_frm.ThreadCount;
                t_book_mgr.ComName = treeSite.SelectedNode.ToolTipText.ToString();
                t_book_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

                Thread thd = new Thread(t_book_mgr.StartGoogleSearch);
                if (m_dic.ContainsKey(t_site_id) == false)
                {
                    m_dic.Add(t_site_id, thd);
                }
                thd.Start();
            }
          
        }
    }
}
